package com.tianda.his.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.system.dto.RoleDto;
import com.tianda.his.modules.system.entity.SysRole;
import com.tianda.his.modules.system.entity.SysUser;
import com.tianda.his.modules.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author jinken
 * @since 2025-09-12
 */
@RestController
public class SysRoleController {

    @Autowired
    protected ISysRoleService sysRoleService;

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("/system/role/selectAllRole")
    public AjaxResult selectAllRole() {
        List<SysRole> list = sysRoleService.list();
        return AjaxResult.success(list);
    }

    /**
     * 根据用户id查询角色id
     * @param userId
     * @return
     */
    @GetMapping("/system/role/getRoleIdsByUserId/{userId}")
    public AjaxResult getRoleIdsByUserId(@PathVariable Long userId) {
        List<Long> roleIds = sysRoleService.getRoleIdsByUserId(userId);
        return AjaxResult.success(roleIds);
    }

    /**
     * 给用户分配角色
     * @param userId 用户id
     * @param roleIds 角色id集合
     * @return
     */
    //6,7,8,1,2,3,4,5,11
    @PostMapping("/system/role/saveRoleUser/{userId}/{roleIds}")
    public AjaxResult saveRoleUser(@PathVariable Long userId, @PathVariable List<Long> roleIds) {
        sysRoleService.saveRoleUser(userId,roleIds);
        return AjaxResult.success();
    }


    @GetMapping("/system/role/listRoleForPage")
    public AjaxResult listRoleForPage(RoleDto roleDto) {
        Page<SysRole> pageResult = sysRoleService.listRoleForPage(roleDto);
        return AjaxResult.success(pageResult.getRecords(), pageResult.getTotal());
    }



}
